<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%@page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,java.io.ByteArrayOutputStream" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<%
	 long serialVersionUID = 1L;  
	  
	  int WIDTH = 65;//设置图片的宽度  
	  
	 int HEIGHT = 22;//设置图片的高度    
	  response.setContentType("image/jpeg");  
	  ServletOutputStream sos = response.getOutputStream();  
	  
	  response.setHeader("Pragma", "No-cache");  
	  response.setHeader("Cache-Control", "no-cache");  
	  response.setDateHeader("Expires", 0);  
	  
	  BufferedImage image = new BufferedImage(WIDTH, HEIGHT,  
	    BufferedImage.TYPE_INT_RGB);  
	  Graphics g = image.getGraphics();  
	  
	  char[] rands = generateCheckCode();  
	  drawBackground(g);  
	  drawRands(g, rands);  
	  g.dispose();  
	  
	  ByteArrayOutputStream bos = new ByteArrayOutputStream();  
	  ImageIO.write(image, "JPEG", bos);  
	  byte[] buf = bos.toByteArray();  
	  response.setContentLength(buf.length);  
	  sos.write(buf);  
	  bos.close();  
	  sos.close();  
	  session.setAttribute("randomCode", new String(rands));  
	%>
	<%!
	   private int WIDTH = 65;//设置图片的宽度  
	   
	  private  int HEIGHT = 22;//设置图片的高度    
	private void drawBackground(Graphics g) {  
		  g.setColor(new Color(0xDCDCDC));  
		  g.fillRect(0, 0, WIDTH, HEIGHT);  
		  for (int i = 0; i < 120; i++) {  
		   int x = (int) (Math.random() * WIDTH);  
		   int y = (int) (Math.random() * HEIGHT);  
		   int red = (int) (Math.random() * 255);  
		   int green = (int) (Math.random() * 255);  
		   int blue = (int) (Math.random() * 255);  
		   g.setColor(new Color(red, green, blue));  
		   g.drawOval(x, y, 1, 0);  
		  }  
		 }  
		  
		 private void drawRands(Graphics g, char[] rands) {  
		  // g.setColor(Color.BLUE);  
		  Random random = new Random();  
		  int red = random.nextInt(110);  
		  int green = random.nextInt(50);  
		  int blue = random.nextInt(50);  
		  g.setColor(new Color(red, green, blue));  
		  g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));  
		  g.drawString("" + rands[0], 1, 17);  
		  g.drawString("" + rands[1], 16, 15);  
		  g.drawString("" + rands[2], 31, 18);  
		  g.drawString("" + rands[3], 46, 16);  
		 }  
		  
		 private char[] generateCheckCode() {  
		  String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  
		  char[] rands = new char[4];  
		  for (int i = 0; i < 4; i++) {  
		   int rand = (int) (Math.random() * 36);  
		   rands[i] = chars.charAt(rand);  
		  }  
		  return rands;  
		 }  
	%>
</body>
</html>